<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="项目名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="项目状态" clearable>
          <el-option
            v-for="dict in dict.type.project_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="项目状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="项目状态" clearable>
          <el-option
            v-for="dict in dict.type.project_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:taskShow:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      
      <el-col :span="1.5">
        <el-button
          @click="gantetu()"
          type="warning"
          plain
          icon="el-icon-s-data"
          size="mini"
        >甘特图
         </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="taskShowList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
    <el-table-column prop="title" label="项目名称" :show-overflow-tooltip="true" width="160"></el-table-column>
      <el-table-column prop="detail" label="项目详情" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="priority" label="项目优先级">
       <template slot-scope="scope">
          <dict-tag :options="dict.type.project_priority" :value="scope.row.priority"/>
        </template>
      </el-table-column>
      <el-table-column prop="createName" label="创建人" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="updateName" label="修改人" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="taskStartDate" label="预计开始时间" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="taskEndDate" label="预计结束时间" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column
      prop="schedulePercent"
      label="进度百分比"
      width="100"
      >
      <template slot-scope="scope">
        <el-tag
          :type="scope.row.tag === 'success'"
          disable-transitions>{{scope.row.schedulePercent}}</el-tag>
      </template>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.tag === 'success'">
            <dict-tag :options="dict.type.project_status" :value="scope.row.state" />
          </el-tag> 
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button 
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:taskShow:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:taskShow:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:taskShow:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改菜单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="父级项目名称">
              <treeselect
                v-model="form.parentId"
                :options="taskShowOptions"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="选择上级项目"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="项目名称" prop="title">
              <el-input v-model="form.title" placeholder="请输入项目名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="项目详情" prop="detail">
              <el-input v-model="form.detail" type="textarea" placeholder="请输入项目详情" :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item>
              <span slot="label">
                <el-tooltip content="选择隐藏则路由将不会出现在侧边栏，但仍然可以访问" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                项目优先级
              </span>
              <el-radio-group v-model="form.priority">
                <el-radio
                  v-for="dict in dict.type.project_priority"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item>
              <span slot="label">
                <el-tooltip content="选择隐藏则路由将不会出现在侧边栏，但仍然可以访问" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                项目状态
              </span>
              <el-radio-group v-model="form.state">
                <el-radio
                  v-for="dict in dict.type.project_status"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预计开始时间" prop="taskStartDate">
            <el-date-picker clearable
              v-model="form.taskStartDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择预计开始时间">
            </el-date-picker>
          </el-form-item></el-col>
          <el-col :span="12">
            <el-form-item label="预计结束时间" prop="taskEndDate">
            <el-date-picker clearable
              v-model="form.taskEndDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择预计结束时间">
              </el-date-picker>
              </el-form-item>
            </el-col>
          <el-col :span="12">
            <el-form-item label="处理人" prop="createName">
              <el-input v-model="form.createName" placeholder="请输入处理人" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTaskShow, getTaskShow, delTaskShow, addTaskShow, updateTaskShow } from "@/api/factory/taskShow";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import IconSelect from "@/components/IconSelect";
import { getData } from "../../../api/system/dict/data";

export default {
  name: "taskShow",
  dicts: ['sys_show_hide', 'sys_normal_disable', 'project_priority', 'project_status'],
  components: { Treeselect, IconSelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 菜单表格树数据
      taskShowList: [],
      // 菜单树选项
      taskShowOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        title: undefined,
        state: undefined,
        visible: undefined
      },
       // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        taskStartDate: [
          { required: true, message: "项目优先级不能为空", trigger: "blur" }
        ],
        taskEndDate: [
          { required: true, message: "项目状态不能为空", trigger: "blur" }
        ],
        createName: [
          { required: true, message: "处理人名称不能为空", trigger: "blur" }
        ],
        taskStartDate: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        taskEndDate: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.createDate();
    this.getList();
  },
  methods: {
    // 甘特图
    gantetu(){
      this.$router.push('/factory/task-gante/gante')
    },
    // 选择图标
    selected(name) {
      this.form.icon = name;
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = true;
      listTaskShow(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.taskShowList = this.handleTree(response.data, "id");
        this.loading = false;
      });
    },
    createDate() {
      var nowDate = new Date(new Date().getTime()-1000*60*60*24*30);
      var nowDate2 = new Date(new Date().getTime()+1000*60*60*24);
      var startdate = {
        year: nowDate.getFullYear(),
        month: nowDate.getMonth() + 1,
        day: nowDate.getDate(),
      }
        var endDate = {
        year: nowDate2.getFullYear(),
        month: nowDate2.getMonth() + 1,
        day: nowDate2.getDate(),
      }
      var startdate = startdate.year + '-' + (startdate.month >= 10 ? startdate.month : '0' + startdate.month) + '-' + (startdate.day >= 10 ? startdate.day : '0' + startdate.day);
      var endDate = endDate.year + '-' + (endDate.month >= 10 ? endDate.month : '0' + endDate.month) + '-' + (endDate.day >= 10 ? endDate.day : '0' + endDate.day);
      this.dateRange = [startdate,endDate];
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.title,
        children: node.children
      };
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      listTaskShow().then(response => {
        this.taskShowOptions = [];
        const taskShow = { id: 0, title: '主类项目', children: [] };
        taskShow.children = this.handleTree(response.data, "id");
        this.taskShowOptions.push(taskShow);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        parentId: 0,
        title: undefined,
        detail: undefined,
        priority: "0",
        state: "0",
        parenttitle: undefined,
        createName: undefined,
        visible: undefined,
        status: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.createDate();
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.id) {
        this.form.parentId = row.id;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加项目";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      getTaskShow(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改菜单";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateTaskShow(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTaskShow(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除名称为"' + row.title + '"的数据项？').then(function() {
        return delTaskShow(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
